package com.lc.luzhengtang.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.bean.GoodsPropertyBean;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${junLong} on 2017/4/17.
 */

public class ShowGoodsPropertyPopu extends PopupWindow implements View.OnClickListener{
    private View view;
    private List<GoodsPropertyBean> list = new ArrayList<>();
    private List<String> propertyList = new ArrayList<>();

    private TextView tvGoodsNum;
    private int num = 1;
    private int state;
    public ShowGoodsPropertyPopu(int state, Context context, String goodsName, String goodsPrice, String pictureUrl, List<String> propertyList, View.OnClickListener listener) {
        this.propertyList = propertyList;
        this.state=state;
        view = LayoutInflater.from(context).inflate(R.layout.popu_goods_property_layout, null);
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) view);
        initView(goodsName,goodsPrice,pictureUrl,context);
        // 设置外部可点击
        this.setOutsideTouchable(true);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(R.id.pop_layout).getTop();

                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return false;
            }
        });

          /* 设置弹出窗口特征 */
        // 设置视图
        this.setContentView(this.view);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);

        // 设置弹出窗体可点击
        this.setFocusable(true);

        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x80000000);
        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);

        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.take_photo_anim);
        @SuppressLint("WrongViewCast") Button btEnsure = (Button) view.findViewById(R.id.rl_ensure_digital);
        btEnsure.setOnClickListener(listener);
        switch (state) {
            case 0:
                btEnsure.setText("加入购物车");
                break;
            case 1:
                btEnsure.setText("加入购物车");
                break;
        }

    }
    private void initView(String goodsName, String goodsPrice, String picUrl, final Context context) {
        RecyclerView rvList = (RecyclerView) view.findViewById(R.id.rv_list);
        rvList.setLayoutManager(new FullyLinearLayoutManager(context));

        //添加头部 尾部
        View headerView = View.inflate(context, R.layout.popu_goods_header_layout, null);
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) headerView);
        ImageView ivShowGoods = (ImageView) headerView.findViewById(R.id.iv_show_goods);
        Glide.with(context).load(picUrl).into(ivShowGoods);
        ImageView ivClose = (ImageView) headerView.findViewById(R.id.iv_close);
        ivClose.setOnClickListener(this);
        TextView tvGoodsTitle = (TextView) headerView.findViewById(R.id.tv_goods_title);
        tvGoodsTitle.setText(goodsName);
        TextView tvGoodsPrice = (TextView) headerView.findViewById(R.id.tv_goods_price);
        tvGoodsPrice.setText(goodsPrice);
        View footerView = View.inflate(context,R.layout.popu_goods_footer_layout,null);
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) footerView);
        ImageButton ibUp = (ImageButton) footerView.findViewById(R.id.ib_up);
        ImageButton ibDown = (ImageButton) footerView.findViewById(R.id.ib_down);
        tvGoodsNum = (TextView) footerView.findViewById(R.id.tv_goods_num);
        tvGoodsNum.setText(String.valueOf(num));
        ibUp.setOnClickListener(this);
        ibDown.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_down:
                if (num>1){
                    num = num - 1;
                    tvGoodsNum.setText(String.valueOf(num));
                }
                break;
            case R.id.ib_up:
                num = num + 1;
                tvGoodsNum.setText(String.valueOf(num));
                break;
            case R.id.iv_close:
                dismiss();
                break;
        }
    }

}
